import api from '../../api';
import { message } from 'antd';

export default {
  namespace: 'ProductManageM',

  state: {
    tableData: { columns: [], dataSource: [] },
  },

  effects: {
    // 获取列表信息
    *getTableData({ payload, callback }, { call, put }) {
      const res = yield call(api.ProductManage.findAll, payload);
      if (res && res.code == 0) {
        yield put({
          type: 'save',
          payload: {
            tableData: res.data,
          },
        });
      } else {
        message.error(res ? res.msg : '列表查询失败!');
      }
    },

    // 新增&编辑
    *addAndEdit({ payload, callback }, { call, put }) {
      const res = yield call(api.ProductManage.addAndEdit, payload);
      if (res && res.code == 0) {
        message.success('操作成功!');
        callback(res);
      } else {
        message.error(res ? res.msg : '操作失败!');
      }
    },
    // 删除
    *deleteData({ payload, callback }, { call, put }) {
      const res = yield call(api.ProductManage.deleteData, payload);
      if (res && res.code == 0) {
        message.success('删除成功!');
        callback(res);
      } else {
        message.error(res ? res.msg : '删除失败!');
      }
    },
    // 获取产品相关信息
    *queryProductInfo({ payload, callback }, { call, put }) {
      const res = yield call(api.ProductManage.queryById, payload);
      if (res && res.code == 0) {
        let arr = res.data ? res.data.dataSource : undefined;
        callback(arr);
      } else {
        message.error(res ? res.msg || res.mesg : '产品信息获取失败!');
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
